package com.neolix.watch.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neolix.watch.NeolixApplication;
import com.neolix.watch.R;
import com.neolix.watch.StatisticsActivity;
import com.neolix.watch.util.FontTypefaceUtil;

import static com.neolix.watch.NeolixApplication.getInstance;

public class MainTitleView extends LinearLayout {

    private MagicTextView currentStepsTextView;
    private TextView stepTitleTextView;
    private TextView goalStepTextView;
    private ImageView walkImageView;
    private AnimationDrawable walkAnim;

    public MainTitleView(Context context) {
        super(context);
        initUi();
        initListener();
    }

    public MainTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUi();
        initListener();
    }

    public MainTitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initUi();
        initListener();
    }

    protected void initUi() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View container = inflater.inflate(R.layout.view_main_title, this, true);

        currentStepsTextView = (MagicTextView) container.findViewById(R.id.current_steps);
        stepTitleTextView = (TextView) container.findViewById(R.id.steps_title);
        goalStepTextView = (TextView) container.findViewById(R.id.goal_steps);
        walkImageView = (ImageView) container.findViewById(R.id.walk_image);

        setTypeface();
    }

    private void initListener() {
        currentStepsTextView.setChangeListener(new MagicTextView.MagicTextViewChangListener() {
            @Override
            public void beginChange() {
                walkAnim.start();
            }

            @Override
            public void completeChange() {
                walkAnim.stop();
                walkImageView.setImageResource(R.drawable.walk_0);
            }
        });
    }

    private void setTypeface() {
        FontTypefaceUtil.setTypefaceOfZagRegular(currentStepsTextView);
        FontTypefaceUtil.setTypefaceOfZagRegular(stepTitleTextView);
        FontTypefaceUtil.setTypefaceOfZagRegular(goalStepTextView);
    }

    public void setCurrentStepValue(int steps) {
        walkImageView.setImageResource(R.drawable.animation_walk);
        walkAnim = (AnimationDrawable) walkImageView.getDrawable();

        currentStepsTextView.setValue(steps);
        currentStepsTextView.scrollChanged();
    }

    public void setCurrentStepText(int steps) {
        currentStepsTextView.setText(String.valueOf(steps));
    }

    public void setGoalStep(int steps) {
        goalStepTextView.setText(String.valueOf(steps));
    }
}
